package com.visizen.wisdomclass.server.action.User;

import com.alibaba.fastjson.JSONObject;
import com.visizen.wisdomclass.server.action.response.Result;
import com.visizen.wisdomclass.server.annotation.ValidateToken;
import com.visizen.wisdomclass.server.model.WcUser;
import com.visizen.wisdomclass.server.model.dto.FamilyExt;
import com.visizen.wisdomclass.server.service.FamilyService;
import com.visizen.wisdomclass.server.token.WCToken;
import com.visizen.wisdomclass.server.token.WCTokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 【用户】获取当前用户的家长信息列表
 *
 * @author: chenjian
 */
@Controller
@RequestMapping("/User")
public class UserGetFamilyListAction {
    /**
     * 获取当前用户的家长信息列表，将当前用户的USERID作为studentid到wc_family表中查询
     *
     * @param token
     * @param request
     * @return
     */
    @ValidateToken
    @RequestMapping(value = "/getFamilyList", method = RequestMethod.GET)
    @ResponseBody
    public Result getFamilyList(@RequestParam(required = true) String token,
                                HttpServletRequest request) {
        Result result = new Result();
        JSONObject jo = new JSONObject();
        WCToken wcToken = tokenManager.getToken(token);

        WcUser student = (WcUser) wcToken.getSource();
        List<FamilyExt> families = familyService.getFamilyByStudentId(student.getUser_id());
        if (null == families) {
            /**
             * 获取家长信息失败
             */
        } else {
            /**
             * 成功获取到家长的信息，设置前台所需要的信息返回
             */
            jo.put("resultCount", families.size());
            List<JSONObject> familyList = new ArrayList<JSONObject>();
            JSONObject family = null;
            WcUser parent;
            for (FamilyExt f : families) {
                family = new JSONObject();
                parent = f.getUser();
                family.put("userId", parent.getUser_id());
                family.put("fullname", parent.getFullname());
                family.put("sex", parent.getSex());
                family.put("mobilephone", parent.getMobilephone());
                family.put("email", parent.getEmail());
                family.put("relation", f.getRelation());
                familyList.add(family);
            }
            jo.put("familyList", familyList);
            result.setSuccessResult(request, "", jo);
        }
        return result;
    }

    private WCTokenManager tokenManager;
    private FamilyService familyService;

    @Autowired
    public void setFamilyService(FamilyService familyService) {
        this.familyService = familyService;
    }

    @Autowired
    public void setTokenManager(WCTokenManager tokenManager) {
        this.tokenManager = tokenManager;
    }
}
